<?php
namespace app\admin\validate;

use think\Validate;
use app\admin\model\WorkerDepartment as WDM;

class DepartValidate extends Validate
{
    protected $rule = [
        'name' => 'require|checkName:name'
    ];

    protected $message = [
        'name.require' => '部门名称不能为空',
        'name.checkName' => '部门已经存在'
    ];

    protected $scene = [
        'savedata' => ['name']
    ];

    /**
     * [checkName 检查部门名称是否存在]
     * @param  [type] $value [description]
     * @param  [type] $rule  [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function checkName($value, $rule, $data)
    {
        if (isset($data['id']) && $data['id'] > 0) {
            $departmentId = WDM::where('id', '<>', $data['id'])->where('name', $value)->value('id');
        } else {
            $departmentId = WDM::where('name', $value)->value('id');
        }
         if ($departmentId > 0) {
             return false;
         }
         return true;
    }
}
